package cn.xfdzx.android.apps.shop.sql;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;


import java.util.ArrayList;

public class DatabaseAdaper {

    private DatabaseHelper databaseHelper;
    public DatabaseAdaper(Context context){
        databaseHelper=new DatabaseHelper(context);
    }
    //添加操作
    public synchronized  void add(ArrayList<String>  list){
        SQLiteDatabase db=databaseHelper.getWritableDatabase();
        db.execSQL("delete from " + PetMetaData.GameTable.TABLE_NAME);

        for (String searchBeen:list){
            ContentValues values=new ContentValues();
            values.put(PetMetaData.GameTable.SEARCH_HISTORY,searchBeen);
            db.insert(PetMetaData.GameTable.TABLE_NAME,null,values);
        }
        db.close();
    }



    //查询所有
    public synchronized ArrayList<String> findAll(){
        SQLiteDatabase db=databaseHelper.getReadableDatabase();
        String[] colums={ PetMetaData.GameTable.SEARCH_HISTORY};
        //是否去除重复记录，参数（表名，要查询的列，查询条件，查询条件的值，分组条件，分组条件的值，排序，分页）
        Cursor c=db.query(true, PetMetaData.GameTable.TABLE_NAME,colums,null,null,null,null,null,null);
        ArrayList<String> searchBeens=new ArrayList<>();
        while (c.moveToNext()){
            searchBeens.add(c.getString(c.getColumnIndexOrThrow(PetMetaData.GameTable.SEARCH_HISTORY)));
        }
        c.close();
        db.close();
        return searchBeens;
    }

}
